/*
串口中断发送函数库
建立循环队列

移植要点
在main初始化时调用函数UartValInit
在定时1ms中调用函数UartSetTime
在主函数里调用函数UartEvent


*/
#include "user.h"



extern void UartSetTime(unsigned char dat);
extern void UartEvent(void);
extern int UartAdd(unsigned char *dat,unsigned char len);
extern void uart_rx_irq_del(unsigned char dat);
extern void uart_tx_irq_del(unsigned char dat);


typedef struct
{
	//unsigned char port;//串口号
 	unsigned char send_post;//当前发送位置
	unsigned char end_post;//数据结尾位置  
}UART_MSG;

//缓冲区大小
#define UartBufMax 50
unsigned char nUart1TxBuf[UartBufMax]={0};

static unsigned char nUart1RxBuf[UartBufMax]={0};
//串口变量
unsigned char busy=0;//发送控制位
unsigned char bRxFlag=0;//接受控制位
unsigned char bRxTim1ms=0;//串口1ms软时基
unsigned char nRxlen=0;//接收数据长度
unsigned char nRxTimCnt=0;//接收计时
/*
*数据首地址
*数据长度
*/

UART_MSG pUartMsg1;

/*----------------------------
发送串口数据
----------------------------*/
//void SendData(unsigned char dat)
//{
//    while (busy);               //µÈ´ýÇ°ÃæµÄÊý¾Ý·¢ËÍÍê³É
//    busy = 1;
//    S0BUF = dat;                 //Ð´Êý¾Ýµ½UARTÊý¾Ý¼Ä´æÆ÷
//}


/*1ms 串口软时基*/
void UartSetTime(unsigned char dat)
{
    bRxTim1ms=dat;
}

/* 串口数据初始化 */
void UartValInit(void)
{
    pUartMsg1.end_post=0;
    pUartMsg1.send_post=0;
    bRxFlag=0;
    nRxlen=0;
    busy=0;
    bRxTim1ms=0;
}

/*填入串口数据，返回-1是溢出
*/
int UartAdd(unsigned char *dat,unsigned char len)
{
    unsigned char i=0;
    unsigned char lesscnt=0;
    //首先计算剩余空间
    if(pUartMsg1.end_post>=pUartMsg1.send_post)
        lesscnt=UartBufMax-pUartMsg1.end_post + pUartMsg1.send_post;
    else
        lesscnt =  pUartMsg1.send_post-pUartMsg1.end_post;
   
    if(len>lesscnt)return -1;
    
    for(i=0;i<len;i++)
    {   
        if(pUartMsg1.end_post>=pUartMsg1.send_post)
        {//正序填入
            if(pUartMsg1.end_post<UartBufMax)
            {
                nUart1TxBuf[pUartMsg1.end_post++] = *dat++;
            }
            else 
                pUartMsg1.end_post=0;//回到起始
        }
        else
        {//环形对接
            if(pUartMsg1.end_post<pUartMsg1.send_post)
            {
                nUart1TxBuf[pUartMsg1.end_post++] = *dat++;
            }
        }
    }
    return 0;
}


/*接收处理函数
*/
unsigned char nSaveWorkStat=0;
void UartProcess(void)
{
    unsigned char i=0;
    
    for(i=0;i<nRxlen;i++)
    {       
        //SendData(nRxlen);
        if((nUart1RxBuf[i]==0xaa)&&(nUart1RxBuf[i+1]==0x55))
        {
           //用户处理函数
        }
        else
        {
           
        }
    }
}

/*串口处理主函数*/
void UartEvent(void)
{
   //串口空闲中断10ms
    if(bRxTim1ms)
    {
       bRxTim1ms=0;
       if(nRxlen!=0)
       {
           nRxTimCnt++;
           if(nRxTimCnt>20)
           {//10ms空闲中断
               nRxTimCnt = 0;
               bRxFlag = 1;
           }
       }          
    }

    //接收处理函数
    if(bRxFlag)
    {
        UartProcess();  
        //UartAdd(nUart1RxBuf,nRxlen);
        bRxFlag=0;
        nRxlen=0;
    }    
    
    //发送函数
    if(pUartMsg1.end_post==pUartMsg1.send_post)
    {
        return;
    }
    if(!busy)
    {  
        //发送位置-使用用户自己的发送函数
        S0BUF = nUart1TxBuf[pUartMsg1.send_post++];
        
        busy=1;
        if(pUartMsg1.end_post<pUartMsg1.send_post)
        {
            if(pUartMsg1.send_post>=UartBufMax)pUartMsg1.send_post=0;//从头开始     
        }
        else if(pUartMsg1.send_post==pUartMsg1.end_post)
        {
            BuzzSet(SOUND_NULL);
            pUartMsg1.send_post=0;
            pUartMsg1.end_post=0;
        }
    }
}
/*----------------------------
UART 中断服务函数
-----------------------------*/
void uart_rx_irq_del(unsigned char dat)
{
	if(bRxFlag==0)
	{
			nRxTimCnt = 0;
			//接收数据--使用用户自己的接收函数
			nUart1RxBuf[nRxlen++] = dat;
			if(nRxlen>=UartBufMax)
			{
					bRxFlag = 1;
			}
	}
}

void uart_tx_irq_del(unsigned char dat)
{
	busy = 0;               //清除标志
}
//void Uart0_IRQHandler() interrupt 4
//{ 
//    SFRPI = 0;
//    if (RI0)
//    {
//        RI0 = 0;                 //清除RI位
//        if(bRxFlag==0)
//        {
//            nRxTimCnt = 0;
//            //接收数据--使用用户自己的接收函数
//            nUart1RxBuf[nRxlen++] = S0BUF;
//            if(nRxlen>=UartBufMax)
//            {
//                bRxFlag = 1;
//            }
//        }
//    }
//    if (TI0)
//    {
//        TI0 = 0;                 //清除TI位
//        busy = 0;               //清除标志
//    }   
//}